package com.roadjava.rbac.service.impl;

import com.roadjava.rbac.bean.entity.IndexRes;
import com.roadjava.rbac.bean.req.resource.AddResParam;
import com.roadjava.rbac.mapper.IndexResMapper;
import com.roadjava.rbac.service.IndexResService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Service
public class IndexResServiceImpl implements IndexResService {
    @Resource
    private IndexResMapper indexResMapper;
    @Value("${upload.static-dir}")
    private String getUploadDir;

    private static final String UPLOAD_DIR = "uploads";

    public String uploadFile(MultipartFile file) throws IOException {

        // 创建文件保存目录
//        String uploadDir = System.getProperty("user.dir") + File.separator + UPLOAD_DIR;
        String uploadDir = getUploadDir + File.separator + UPLOAD_DIR;
        Path uploadPath = Paths.get(uploadDir);
//        Path uploadPath = Paths.get(UPLOAD_DIR);
        if (!Files.exists(uploadPath)) {
            Files.createDirectories(uploadPath);
        }

        // 获取文件名
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            throw new IllegalArgumentException("File name cannot be null");
        }

        // 保存文件
        Path filePath = uploadPath.resolve(fileName);
        System.out.println("Uploading to: " + filePath);
        file.transferTo(filePath.toFile());

        // 将文件信息存入数据库
//        IndexRes indexRes = new IndexRes();
//        indexRes.setName(fileName);
//        indexRes.setPath(UPLOAD_DIR+"/"+fileName);
//        indexResMapper.insert(indexRes);

        return fileName;
    }

    @Override
    public List<IndexRes> getAllFiles() {
        return indexResMapper.getAllFiles();
    }
    @Override
    public String deleteFile(Integer fileId) throws IOException {
        // 根据文件ID获取文件信息
        IndexRes indexRes = indexResMapper.getFileById(fileId);
        if (indexRes == null) {
            return "File not found";
        }

        // 删除数据库中的记录
        indexResMapper.deleteFileById(fileId);

        // 删除物理文件
        Path filePath = Paths.get(getUploadDir+indexRes.getPath());
        File file = filePath.toFile();
        if (file.exists()) {
            boolean deleted = file.delete();
            if (deleted) {
                return "File deleted successfully";
            } else {
                return "Failed to delete the file from the server";
            }
        } else {
            return "File does not exist on the server";
        }
    }

    @Override
    public String addRes(AddResParam param) {
        IndexRes indexRes = new IndexRes();
        indexRes.setName(param.getFileName());
        indexRes.setPath(UPLOAD_DIR+"/"+param.getFileName());
        indexRes.setLink(param.getLink());
        indexRes.setType(param.getType());
        indexResMapper.insert(indexRes);
        return "添加成功";
    }

    @Override
    public List<IndexRes> getResByType(Integer param) {
        return indexResMapper.getResByType(param);
    }
}
